/**
 * File Name   : FileUploader.java
 * Description : 
 */
package com.hh.common.util.upload;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.log4j.Logger;
import org.apache.struts.upload.FormFile;

/**
 * 文件上传工具类
 * @author 冯昌硕
 * @create 2013-1-27 上午12:31:17
 */
public class FileUploader {
    
    private static final Logger log = Logger.getLogger(FileUploader.class);
    
    /**
     * 将上传的文件生成到服务器指定目录下
     * @param fileUploadDirStr 要上传的文件在服务器上的目录
     * @param ff 上传文件对象
     * @param newFileName 文件重命名后的新文件名
     * @create 2013-1-27 上午12:32:11
     */
    public static boolean upload(String fileUploadDirStr, FormFile ff, String newFileName) {
        
        InputStream is = null;
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        
        try {
            
            File upDir = new File(fileUploadDirStr);
            if(!upDir.exists()) {
                upDir.mkdirs();
            }
            
            is = ff.getInputStream();
            bis = new BufferedInputStream(is);
            
            File file = new File(fileUploadDirStr + File.separator + newFileName);
            bos = new BufferedOutputStream(new FileOutputStream(file));
            
            int readDataSize = 2048;
            byte[] readData = new byte[readDataSize];
            
            int writeCnt = 0;
            int readSize = -1;
            
            while( (readSize = bis.read(readData)) != -1 ) {
                if(readSize == readDataSize) {
                    bos.write(readData);
                } else {
                    bos.write(readData, 0, readSize);
                }
                
                writeCnt++;
                if(writeCnt >= 5) {
                    bos.flush();
                }
            }
            
            bos.flush();
            return true;
        } catch (FileNotFoundException e) {
            log.error("向[" + fileUploadDirStr + "]目录上传[" + ff.getFileName() + "]文件, FileNotFoundException异常: " + e.toString());
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            log.error("向[" + fileUploadDirStr + "]目录上传[" + ff.getFileName() + "]文件, FileNotFoundException异常: " + e.toString());
            e.printStackTrace();
            return false;
        } finally {
            if(is != null) {
                try {
                    is.close();
                    //log.debug("向[" + fileUploadDirStr + "]目录上传[" + ff.getFileName() + "]文件, is流关闭");
                } catch (IOException e) {
                    log.error("向[" + fileUploadDirStr + "]目录上传[" + ff.getFileName() + "]文件, is流关闭异常: " + e.toString());
                    e.printStackTrace();
                }
            }
            
            if(bis != null) {
                try {
                    bis.close();
                    //log.debug("向[" + fileUploadDirStr + "]目录上传[" + ff.getFileName() + "]文件, bis流关闭");
                } catch (IOException e) {
                    log.error("向[" + fileUploadDirStr + "]目录上传[" + ff.getFileName() + "]文件, bis流关闭异常: " + e.toString());
                    e.printStackTrace();
                }
            }
            
            if(bos != null) {
                try {
                    bos.close();
                    //log.debug("向[" + fileUploadDirStr + "]目录上传[" + ff.getFileName() + "]文件, bos流关闭");
                } catch (IOException e) {
                    log.error("向[" + fileUploadDirStr + "]目录上传[" + ff.getFileName() + "]文件, bos流关闭异常: " + e.toString());
                    e.printStackTrace();
                }
            }
        }
        
    }
    
    /**
     * 删除指定路径的文件
     * @param filePath
     * @create 2013-1-27 上午11:19:19
     */
    public static boolean delete(String filePath) {
        File file = new File(filePath);
        if(!file.exists()) {
            log.debug("要删除的指定文件[" + filePath + "]不存在");
            return true;
        }
        
        if(!file.isFile()) {
            log.debug("要删除的[" + filePath + "]不是文件存在");
            return true;
        }
        
        return file.delete();
    }
    
}
